﻿using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SmlEngine
{
    /// <summary>
    /// Which part of the screen this Viewport is on.
    /// </summary>
    public enum ViewportLocation
    {
        FullScreen,
        TopHalf,
        BottomHalf,
        LeftHalf,
        RightHalf,
        TopLeftQuarter,
        TopRightQuarter,
        BottomLeftQuarter,
        BottomRightQuarter
    }

    public static class GraphicsDeviceExtensions
    {
        #region Blank Texture

        public static bool IsInitialized { get; private set; }

        private static Texture2D blankTexture;
        /// <summary>
        /// Returns a blank texture.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <returns>A blank texture.</returns>
        public static Texture2D GetBlankTexture(this GraphicsDevice graphicsDevice)
        {
            Initialize(graphicsDevice);
            return blankTexture;
        }

        /// <summary>
        /// Initializes this class(?)
        /// </summary>
        /// <param name="graphicsDevice">The GraphicsDevice to use.</param>
        private static void Initialize(GraphicsDevice graphicsDevice)
        {
            if (!IsInitialized)
            {
                blankTexture = new Texture2D(graphicsDevice, 1, 1);
                blankTexture.SetData(new Color[] { Color.White });
                IsInitialized = true;
            }
        }

        #endregion

        #region Viewports
        /// <summary>
        /// Returns a viewport given a location.
        /// </summary>
        /// <param name="device">The GraphicsDevice to use.</param>
        /// <param name="location">The ViewportLocation to use.</param>
        /// <returns>A new viewport from the location</returns>
        public static Viewport GetViewport(
           this GraphicsDevice device,
           ViewportLocation location)
        {
            Viewport viewport = new Viewport();
            viewport.MinDepth = device.Viewport.MinDepth;
            viewport.MaxDepth = device.Viewport.MaxDepth;

            switch (location)
            {
                case ViewportLocation.FullScreen:
                    viewport.X = 0;
                    viewport.Y = 0;
                    viewport.Width = device.PresentationParameters.BackBufferWidth;
                    viewport.Height = device.PresentationParameters.BackBufferHeight;
                    break;
                case ViewportLocation.TopHalf:
                    viewport.X = 0;
                    viewport.Y = 0;
                    viewport.Width = device.PresentationParameters.BackBufferWidth;
                    viewport.Height = device.PresentationParameters.BackBufferHeight / 2;
                    break;
                case ViewportLocation.BottomHalf:
                    viewport.X = 0;
                    viewport.Y = device.PresentationParameters.BackBufferHeight / 2;
                    viewport.Width = device.PresentationParameters.BackBufferWidth;
                    viewport.Height = device.PresentationParameters.BackBufferHeight / 2;
                    break;
                case ViewportLocation.LeftHalf:
                    viewport.X = 0;
                    viewport.Y = 0;
                    viewport.Width = device.PresentationParameters.BackBufferWidth / 2;
                    viewport.Height = device.PresentationParameters.BackBufferHeight;
                    break;
                case ViewportLocation.RightHalf:
                    viewport.X = device.PresentationParameters.BackBufferWidth / 2;
                    viewport.Y = 0;
                    viewport.Width = device.PresentationParameters.BackBufferWidth / 2;
                    viewport.Height = device.PresentationParameters.BackBufferHeight;
                    break;
                case ViewportLocation.TopLeftQuarter:
                    viewport.X = 0;
                    viewport.Y = 0;
                    viewport.Width = device.PresentationParameters.BackBufferWidth / 2;
                    viewport.Height = device.PresentationParameters.BackBufferHeight / 2;
                    break;
                case ViewportLocation.TopRightQuarter:
                    viewport.X = device.PresentationParameters.BackBufferWidth / 2;
                    viewport.Y = 0;
                    viewport.Width = device.PresentationParameters.BackBufferWidth / 2;
                    viewport.Height = device.PresentationParameters.BackBufferHeight / 2;
                    break;
                case ViewportLocation.BottomLeftQuarter:
                    viewport.X = 0;
                    viewport.Y = device.PresentationParameters.BackBufferHeight / 2;
                    viewport.Width = device.PresentationParameters.BackBufferWidth / 2;
                    viewport.Height = device.PresentationParameters.BackBufferHeight / 2;
                    break;
                case ViewportLocation.BottomRightQuarter:
                    viewport.X = device.PresentationParameters.BackBufferWidth / 2;
                    viewport.Y = device.PresentationParameters.BackBufferHeight / 2;
                    viewport.Width = device.PresentationParameters.BackBufferWidth / 2;
                    viewport.Height = device.PresentationParameters.BackBufferHeight / 2;
                    break;
            }

            return viewport;
        }

        /// <summary>
        /// Sets a Viewport to a GraphicsDevice.
        /// </summary>
        /// <param name="device">The GraphicsDevice to use.</param>
        /// <param name="location">The ViewportLocation of the new viewport.</param>
        public static void SetViewport(
            this GraphicsDevice device,
            ViewportLocation location)
        {
            device.Viewport = device.GetViewport(location);
        }

        /// <summary>
        /// Returns a full-screen viewport.
        /// </summary>
        /// <param name="device">The GraphicsDevice to use.</param>
        /// <returns>A full-screen viewport.</returns>
        public static Viewport FullScreen(this GraphicsDevice device)
        {
            return device.GetViewport(ViewportLocation.FullScreen);
        }

        #endregion
    }
}
